package com.cremamobile.filemanager.file;

import java.io.File;

import java.util.List;

import com.cremamobile.filemanager.CremaActivity;
import com.cremamobile.filemanager.CremaProgressDialog;
import com.cremamobile.filemanager.R;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.os.AsyncTask;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.Toast;

public abstract class FileWorker extends AsyncTask<String, Integer, Boolean>
{
	public final static int FILE_COPYER = 301;
	public final static int FILE_MOVER = 300;
	public final static int FILE_DELETER = 302;

	public final static int MODE_NONE = 500;
	public final static int MODE_REPLACE = 501;
	public final static int MODE_RENAME = 502;
	
	protected Aborter mFlag;
	protected Context mContext;
	protected Activity mCaller;
	protected CremaProgressDialog mProgressDialog;
	protected ResultCallbacks mCallerCb;
	
	protected List<FileListEntry> mFiles;
	protected int mFileCount;
	protected int mStep = 0;
	protected boolean mBackgroundMode = false;
	protected int mFailCode = 0;
	
	protected int mWorkMode = MODE_NONE;
	protected int mModeItemCnt = 0;
	
	
	public FileWorker(Context context, List<FileListEntry> files, ResultCallbacks cb) {
		this.mContext = context;
		this.mFlag = new Aborter();
		this.mFiles = files;
		this.mFileCount = (files != null ? files.size() : 0);
		this.mCallerCb = cb;
	}
	
	public void setResultCallback(ResultCallbacks cb) {
		mCallerCb = cb;
	}
	
	public void showWithActivity(Activity caller) {
		this.mCaller = caller;
		mBackgroundMode = false;
	}
	
	abstract public String getPreExecuteDialogMessage(int step);
	
	abstract public String getPostExecuteToastMessage(boolean success);
	
	abstract public Boolean working();

	abstract public String getProgressContentMessageByStep(int step);

	abstract public String getProgressSizeMessageByStep(int step);

	@Override
	protected void onPostExecute(Boolean result) {
		super.onPostExecute(result);
		if(result) {
			if (isShowWithActivity()) {
//				mCaller.runOnUiThread(new Runnable() {
//					
//					@Override
//					public void run() {
						if(mProgressDialog.isShowing()) {
							mProgressDialog.dismiss();
						}
						
						Toast.makeText(mContext, getPostExecuteToastMessage(true), Toast.LENGTH_LONG).show();
//					}
//				});
			}
			
			mCallerCb.onSuccess();
		}
		else {
			if (isShowWithActivity()) {
//				mCaller.runOnUiThread(new Runnable() {
//					
//					@Override
//					public void run() {
						if(mProgressDialog.isShowing()) {
							mProgressDialog.dismiss();
						}
						Toast.makeText(mContext, getPostExecuteToastMessage(false), Toast.LENGTH_LONG).show();
//					}
//				});
			} 
			mCallerCb.onFail(mFailCode);
		}
	}
	
	@Override
	protected Boolean doInBackground(String... params) {
		return working();
	}
	
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		final long totalSize = sumSize(mFiles, 0);

		// 시작...
		
		if ( isShowWithActivity() ) {
//			mCaller.runOnUiThread(new Runnable() {
//				@Override
//				public void run() {
//					
					mProgressDialog = new CremaProgressDialog(mCaller);
					mProgressDialog.setMax(mFiles.size());
					mProgressDialog.setProgress(0);
					mProgressDialog.setContentText(getProgressContentMessageByStep(0));
					mProgressDialog.setContentSizeText(getProgressSizeMessageByStep(0));
					mProgressDialog.setCancelable(true);
					mProgressDialog.setOnCancelListener(new OnCancelListener() {
			            @Override
			            public void onCancel(DialogInterface dialog) {
			                // actually could set running = false; right here, but I'll
			                // stick to contract.
			                cancel(true);
			            }
			        });
					mProgressDialog.show();
//					
//				}
//			});
		}
	}
//	
//	@Override
//    protected void onProgressUpdate(String... progress) {
//		super.onProgressUpdate(progress);
//			mProgressDialog.setProgress(Integer.parseInt(progress[0]));
//    }
	
	public boolean isShowWithActivity() {
		if (! mBackgroundMode && mCaller != null)
			return true;
		return false;
	}
	
	@Override
	protected void onProgressUpdate(Integer... progress) {
		int step = progress[0]+1;
		mProgressDialog.setContentText(getProgressContentMessageByStep(step));
		mProgressDialog.setContentSizeText(getProgressSizeMessageByStep(step));
		mProgressDialog.setProgress(step);
    }
	
	@Override
    protected void onCancelled() {
		mFlag.abort();
    }

	
	public long sumSize(List<FileListEntry> entrys, int index) {
		long sum = 0;
		int count = entrys.size();
		for (int i = index ; i<count; i++) {
			FileListEntry item = entrys.get(i);
			sum += item.getSize();
		}
		
		return sum;
	}
	
	abstract public int type();
	
	public void setMode(int mode) {
		mWorkMode = mode;
	}
	
	abstract public FileListEntry getDestination();
}